<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/31 0031
 * Time: 18:28
 */
namespace app\index\controller;
use app\index\model\prizes;
use app\index\model\Upfiles;
use think\Db;
use think\Request;
//use app\index\model\prizes;

class Prize
{
    /**加载修改奖品页面
     * @param Request $request
     * @return string|\think\response\View
     */
    public function show_updata(Request $request){
        $id = $request->param('id',null);
        if(!$id){
            return '请输入修改的奖品';
        }
        $data = Db::name('prizes')->where('id',$id)->find();
        return view('form/update_p',['data'=>$data]);
    }

    /**更新prizes数据
     * @param Request $request
     * @return int|string
     */
    public function update(Request $request){
        $data = $request->param();
        $file = $_FILES['p_picture'];
        $p_picture = Upfiles::Getone(ROOT_PATH.'public/static/img',$file);
        if($p_picture){
            $data['p_picture'] = $p_picture;
        }
        $datas = Db::name('prizes')->where('id',$data['id'])->find();
        if(prizes::updatePrizes($data)){
            if($datas['p_picture']){
                if(is_file(ROOT_PATH.'public/static/img/'.$datas['p_picture'])){
                    unlink(ROOT_PATH.'public/static/img/'.$datas['p_picture']);
                }
            }
            return 1;
        }else{
            return '数据错误或没发生改变';
        }
    }

    /**删除数据
     * @param Request $request
     * @return int|string
     */
    public function dele(Request $request){
        $id = $request->param('id',null);
        if(!$id){
            return '请输入要删除的数据';
        }
        $res = Db::connect();
        $data = $res->name('prizes')->where('id',$id)->find();
        if($res->name('prizes')->where('id',$id)->delete()){
            if($data['p_picture']){
                if(is_file(ROOT_PATH.'public/static/img/'.$data['p_picture'])){
                    unlink(ROOT_PATH.'public/static/img/'.$data['p_picture']);
                }
            }
            return 1;
        }else{
            return '删除失败';
        }
    }
}